﻿@{
    ViewData["Title"] = "任务管理";
}

<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        任务管理
        <small></small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-map-marker"></i> 主页</a></li>
        <li><a href="#">任务管理</a></li>
        <li class="active">查询</li>
    </ol>
</section>
<!-- Main content -->
<section class="content">
    <!-- Default box -->
    <div class="box">
        <div class="box-header with-border">
            <h3 class="box-title">所有任务</h3>
            <div class="box-tools pull-right">
                <button type="button" class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip"
                        title="Collapse">
                    <i class="fa fa-minus"></i>
                </button>
                <button type="button" class="btn btn-box-tool" data-widget="remove" data-toggle="tooltip" title="Remove">
                    <i class="fa fa-times"></i>
                </button>
            </div>
        </div>
        <!-- /.box-header -->
        <div class="box-body">
            <!-- Main content -->
            <section class="content">
                <div class="row">
                    <div class="col-md-12">
                        <div class="box box-primary">
                            <div class="box-body">
                                <div class="row">
                                    <div class="col-md-12">
                                        <table class="table display dataTable" id="js_table">
                                            <thead>
                                                <tr>
                                                    <th style="text-align:left">任务组-名称</th>
                                                    <th style="text-align:center">触发器类型</th>
                                                    <th style="text-align:center">时间表达式</th>
                                                    @*<th style="text-align:center">重复次数</th>
                                                        <th style="text-align:center">间隔(秒)</th>*@
                                                    <th style="text-align:center">开始时间</th>
                                                    <th style="text-align:center">结束时间</th>
                                                    <th style="text-align:center">触发器状态</th>

                                                    <th style="text-align:center">下次运行时间</th>
                                                    <th style="text-align:center">操作</th>
                                                </tr>
                                            </thead>
                                            <tbody></tbody>
                                        </table>


                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>

            </section>

        </div>
        <!-- /.box-body -->
        <div class="box-footer">
            <div class="col-lg-2">
                <div class="row">
                    <div class="col-md-12">

                        <a class="btn btn-primary" data-toggle="modal" data-backdrop="static" data-keyboard="false" data-target="#modalAdd" onclick="resetForm();">添加任务</a>

                    </div>
                </div>
            </div>

        </div>
        <!-- /.box-footer-->
    </div>
    <!-- /.box -->
</section>

<div class="modal" id="modalAdd" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <form role="form" id="task_form_add">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title"><i class="glyphicon glyphicon-plus"></i><b>添加任务</b></h4>

                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="JobName">任务名称</label>
                        <input type="text" class="form-control" id="jobName" name="JobName" placeholder="请输入名称">
                    </div>
                    <div class="form-group">
                        <label for="JobDescription">任务描述</label>
                        <textarea type="text" class="form-control" rows="2" id="jobDescription" name="JobDescription" placeholder="请输入描述"></textarea>
                    </div>
                    <div class="row ">
                        <div class="form-group col-md-6">
                            <label for="JobGroup">任务组</label>
                            <input type="text" class="form-control" id="jobGroup" name="JobGroup" placeholder="请输入组名称">
                        </div>
                        <div class="form-group col-md-6">
                            <label for="TriggerType">触发器类型</label>
                            <select id="triggerType" name="TriggerType" class="form-control">
                                <option selected="selected" value="2">SIMPLE</option>
                                <option value="1">CRON </option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group" id="cron_div" style="display:none">
                        <label for="Cron">执行周期表达式</label>
                        <input type="text" class="form-control" id="cron" name="Cron" placeholder="请输入表达式">
                    </div>
                    <div class="row">
                        <div class="form-group col-md-6">
                            <label for="BeginTime">开始时间</label>
                            <input type="text" class="form-control" id="beginTime" autocomplete="off" name="BeginTime" placeholder="请选择开始时间">
                        </div>
                        <div class="form-group col-md-6">
                            <label for="EndTime">结束时间</label>
                            <input type="text" class="form-control" id="endTime" autocomplete="off" name="EndTime" placeholder="请选择结束时间">
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-md-6" id="repeatTimes_div">
                            <label for="RunTimes">重复次数(执行次数=重复次数+1)</label>
                            <input type="text" class="form-control" id="repeatTimes" name="RepeatTimes" placeholder="请输入重复次数">
                        </div>
                        <div class="form-group col-md-6" id="interval_div">
                            <label for="Interval">执行间隔时间（秒为单位）</label>
                            <input type="text" class="form-control" id="interval" name="Interval" placeholder="请输入执行间隔">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="AssemblyName">程序集</label>
                        <input type="text" class="form-control" id="assemblyName" name="AssemblyName" placeholder="请输入程序集">
                    </div>
                    <div class="form-group">
                        <label for="ClassName">IJob的实现类</label>
                        <input type="text" class="form-control" id="insideClass" name="ClassName" placeholder="请输入IJob实现类">
                    </div>
                    <div class="form-group">
                        <label for="Params">传入的参数</label>
                        <textarea type="text" class="form-control" id="params" name="Params"></textarea>
                    </div>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="submit" id="save" class="btn btn-primary ladda">保存</button>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>

<div class="modal" id="modalEdit" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <form role="form" id="task_form_edit">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title"><i class="glyphicon glyphicon-edit"></i><b>修改任务</b></h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="JobName">任务名称</label>
                        <input type="text" class="form-control" id="jobName_edit" name="JobName" readonly="readonly" placeholder="请输入名称">
                    </div>
                    <div class="form-group">
                        <label for="">任务描述</label>
                        <textarea type="text" class="form-control" rows="2" id="jobDescription_edit" name="JobDescription" placeholder="请输入描述"></textarea>
                    </div>
                    <div class="row">
                        <div class="form-group col-md-6">
                            <label for="JobGroup">任务组</label>
                            <input type="text" class="form-control" id="jobGroup_edit" name="JobGroup" readonly="readonly" placeholder="请输入组名称">
                        </div>
                        <div class="form-group col-md-6">
                            <label for="TriggerType">触发器类型</label>
                            <select id="triggerType_edit" name="TriggerType" class="form-control">
                                <option value="2">SIMPLE</option>
                                <option value="1">CRON </option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group" id="cron_div_edit" style="display:none">
                        <label for="Cron">执行周期表达式</label>
                        <input type="text" class="form-control" id="cron_edit" name="Cron" placeholder="请输入表达式">
                    </div>
                    <div class="row">
                        <div class="form-group col-md-6">
                            <label for="BeginTime">开始时间</label>
                            <input type="text" class="form-control" id="beginTime_edit" autocomplete="off" name="BeginTime" placeholder="请选择开始时间">
                        </div>
                        <div class="form-group col-md-6">
                            <label for="EndTime">结束时间</label>
                            <input type="text" class="form-control" id="endTime_edit" autocomplete="off" name="EndTime" placeholder="请选择结束时间">
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-md-6" id="repeatTimes_div_edit">
                            <label for="RepeatTimes">重复次数(执行次数=重复次数+1)</label>
                            <input type="text" class="form-control" id="repeatTimes_edit" name="RepeatTimes" placeholder="请输入重复次数">
                        </div>
                        <div class="form-group col-md-6" id="interval_div_edit">
                            <label for="Interval">执行间隔时间（秒为单位）</label>
                            <input type="text" class="form-control" id="interval_edit" name="Interval" placeholder="请输入执行间隔">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="AssemblyName">程序集</label>
                        <input type="text" class="form-control" id="assemblyName_edit" name="AssemblyName" readonly="readonly" placeholder="请输入程序集">
                    </div>
                    <div class="form-group">
                        <label for="ClassName">IJob的实现类</label>
                        <input type="text" class="form-control" id="insideClass_edit" name="ClassName" readonly="readonly" placeholder="请输入IJob实现类">
                    </div>
                    <div class="form-group">
                        <label for="Params">传入的参数</label>
                        <textarea type="text" class="form-control" id="params_edit" name="Params"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="submit" id="save_edit" class="btn btn-primary ladda">保存</button>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>


<div class="modal" id="modalView" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <form role="form" id="task_form_view">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title"><i class="glyphicon glyphicon-eye-open"></i><b>查看任务</b></h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="JobName">任务名称</label>
                        <input type="text" class="form-control" id="jobName_view" readonly="readonly" name="JobName">
                    </div>
                    <div class="form-group">
                        <label for="JobDescription">任务描述</label>
                        <textarea type="text" class="form-control" rows="2" id="jobDescription_view" readonly="readonly" name="JobDescription"></textarea>
                    </div>
                    <div class="row ">
                        <div class="form-group col-md-6">
                            <label for="JobGroup">任务组</label>
                            <input type="text" class="form-control" id="jobGroup_view" readonly="readonly" name="JobGroup">
                        </div>
                        <div class="form-group col-md-6">
                            <label for="TriggerType">触发器类型</label>
                            <select id="triggerType_view" name="TriggerType" readonly="readonly" class="form-control">
                                <option value="2">SIMPLE</option>
                                <option value="1">CRON </option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group" id="cron_div_view" style="display:none">
                        <label for="Cron">执行周期表达式</label>
                        <input type="text" class="form-control" id="cron_view" readonly="readonly" name="Cron">
                    </div>
                    <div class="row">
                        <div class="form-group col-md-6">
                            <label for="BeginTime">开始时间</label>
                            <input type="text" class="form-control" id="beginTime_view" readonly="readonly" name="BeginTime">
                        </div>
                        <div class="form-group col-md-6">
                            <label for="EndTime">结束时间</label>
                            <input type="text" class="form-control" id="endTime_view" readonly="readonly" name="EndTime">
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group col-md-6" id="repeatTimes_div_view">
                            <label for="RunTimes">重复次数(执行次数=重复次数+1)</label>
                            <input type="text" class="form-control" id="repeatTimes_view" readonly="readonly" name="RepeatTimes">
                        </div>
                        <div class="form-group col-md-6" id="interval_div_view">
                            <label for="Interval">执行间隔时间（秒为单位）</label>
                            <input type="text" class="form-control" id="interval_view" readonly="readonly" name="Interval">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="AssemblyName">程序集</label>
                        <input type="text" class="form-control" id="assemblyName_view" readonly="readonly" name="AssemblyName">
                    </div>
                    <div class="form-group">
                        <label for="ClassName">IJob的实现类</label>
                        <input type="text" class="form-control" id="insideClass_view" readonly="readonly" name="ClassName">
                    </div>
                    <div class="form-group">
                        <label for="ClassName">传入的参数</label>
                        <textarea type="text" class="form-control" id="params_view" readonly="readonly" name="Params"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>



<div class="modal fade" id="delcfmOverhaul">
    <div class="modal-dialog">
        <div class="modal-content message_align">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title">提示</h4>
            </div>
            <div class="modal-body">
                <p>您确认要删除该条信息吗？</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">
                    取消
                </button>
                <button type="button" class="btn btn-primary" id="deleteHaulBtn">
                    确认
                </button>
            </div>
        </div>
    </div>
</div>

@section Scripts{
    <script src="~/lib/momentjs/moment.min.js"></script>
    <script>

        const connection = new signalR.HubConnectionBuilder()
            .withUrl("/RefreshTask")
            .configureLogging(signalR.LogLevel.Information)
            .build();

        connection.start().catch(err => console.error(err.toString()));

        $(document).ready(function () {
            $("#beginTime,#endTime,#beginTime_edit,#endTime_edit").datetimepicker({
                format: 'yyyy-mm-dd hh:ii',
                language: 'zh-CN',
                autoclose: true,
                todayBtn: true,
                pickerPosition: "bottom-left",
                startDate: new Date()
            });

            //渲染列表页面初始化
            var t = $("#js_table").DataTable({
                "processing": true,
                "searching": false,//关闭搜索框
                "serverSide": true,//服务器分页
                "bAutoWidth": false,
                "scrollX": true,
                "ajax": {
                    "url": "/Task/GetTasks",
                    "type": "post",
                    "dataSrc": "list",//这里是后台返回的数据对象
                    "data": function (d) {//d 是原始的发送给服务器的数据，默认很长。
                        var param = {};//因为服务端排序，可以新建一个参数对象
                        param.start = d.start;//开始的序号
                        param.length = d.length;//要取的数据的条数
                        return param;//自定义需要传递的参数。
                    }
                },

                columnDefs: [
                    // Center align the header content of column 1
                    { className: "dt-head-center", targets: [0] },
                    // Center align the body content of columns 2, 3, & 4
                    { className: "dt-body-center", targets: [1, 2, 3, 4, 5, 6, 7] }
                ],
                //定义列: 取相应属性字段
                "columns": [
                    {
                        "data": "JobGroup",
                        "bSortable": false,

                        "render": function (data, type, full, callback) {
                            var params = data + "-" + full.JobName;
                            return "<a href='/TaskHistory/Index?namegroup=" + encodeURI(params) + "'>" + params + "</a>";
                        }
                    },
                    {
                        "data": "TriggerType",
                        "render": function (data, type, full, callback) {
                            switch (data) {
                                case 2: return "SIMPLE"; break;
                                case 1: return "CRON"; break;
                                default: return "UNKNOWN"; break;
                            }
                        }
                    },

                    //{ "data": "Cron" },
                    //{ "data": "RepeatTimes" },
                    //{ "data": "Interval" },

                    {

                        "render": function (data, type, full, callback) {

                            switch (full.TriggerType) {
                                case 2: return "<span title='重复次数:[" + full.RepeatTimes + "]间隔:[" + full.Interval + "]秒'>[" + full.RepeatTimes + "]" + "-[" + full.Interval + "]</span>"; break;
                                case 1: return full.Cron; break;
                                default: return ""; break;
                            }


                        }
                    },

                    {
                        "data": "BeginTime",
                        "render": function (data, type, full, callback) {
                            return window.moment(data).format("YYYY-MM-DD HH:mm:ss");
                        }
                    },
                    {
                        "data": "EndTime",
                        "render": function (data, type, full, callback) {
                            if (data != null) {
                                return data != "0001-01-01T00:00:00" ? window.moment(data).format("YYYY-MM-DD HH:mm:ss") : null;
                            }
                            return data;
                        }
                    },
                    {
                        "data": "TriggerState",
                        "render": function (data, type, full, callback) {
                            switch (data) {
                                case 0: return "正常"; break;
                                case 1: return "暂停"; break;
                                case 2: return "完成"; break;
                                case 3: return "异常"; break;
                                case 4: return "阻塞"; break;

                                default: return "失效"; break;
                            }

                        }
                    },

                    {
                        "data": "NextFireTime",
                        "render": function (data, type, full, callback) {
                            if (data) {
                                return window.moment(data).format("YYYY-MM-DD HH:mm:ss");
                            } else {
                                return "";
                            }
                        }
                    },

                    //操作列
                    {

                        "render": function (data, type, full, callback) {

                            var stop = (full.TriggerState != 0 && full.TriggerState != 4 ) ? "disabled" : "";
                            var resume = full.TriggerState != 1 ? "disabled" : "";

                            return (`<a class="btn btn-primary btn-sm " data-toggle="modal" data-target="#modalView" onclick="viewDetail('${full.JobName}','${full.JobGroup}');">查看</a>
                                                                                                                                <a class="btn btn-default btn-sm " data-toggle="modal" data-target="#modalEdit" onclick="editDetail('${full.JobName}','${full.JobGroup}');">编辑</a>
                                                                                                                                <a class="btn btn-warning btn-sm ${stop}"  onclick="stopTask('${full.JobName}','${full.JobGroup}');" >暂停</a>
                                                                                                                                <a class="btn btn-info btn-sm ${resume}"  onclick="resumeTask('${full.JobName}','${full.JobGroup}');" >恢复</a>
                                                                                                                                <a class="btn btn-danger btn-sm" data-toggle="modal" data-target="#delcfmOverhaul" onclick="deleteTask('${full.JobName}','${full.JobGroup}');" >删除</a>`
                            );
                        }
                    }
                ],
                "language": {
                    "lengthMenu": "每页 _MENU_ 条记录",
                    "zeroRecords": " ",
                    "info": "当前 _START_ 条到 _END_ 条 共 _TOTAL_ 条",
                    "infoEmpty": "无记录",
                    "infoFiltered": "(从 _MAX_ 条记录过滤)",
                    "search": "全文搜索",
                    "processing": "载入中",
                    "paginate": {
                        "first": "首页",
                        "previous": "上一页",
                        "next": "下一页",
                        "last": "尾页"
                    }
                },
                "aLengthMenu": [
                    [10, 25, 50, 100], [10, 25, 50, 100]
                ],
                //"aoColumnDefs": [{ "bSortable": false, "aTargets": [2, 3, 4, 5, 6, 7, 8, 11] }],
                "paging": true,//分页
                "pagingType": "full_numbers",//显示首页尾页
                "ordering": false,//排序
                "info": true,//信息
                "initComplete": function (settings, data) {

                },
                "drawCallback": function (settings, data) {

                }

            });
            //参数校验
            $('#task_form_add').validate({
                focusInvalid: false,

                rules: {
                    JobName: {
                        required: true
                    },
                    JobGroup: {
                        required: true
                    },
                    BeginTime: {
                        required: true
                    },
                    AssemblyName: {
                        required: true
                    }
                    ,
                    ClassName: {
                        required: true
                    }
                },
                messages: {

                },
                submitHandler: function (form) {
                    console.log($(form).serialize());
                    $.ajax({
                        type: "POST",
                        url: "/Task/AddTask",


                        data: $(form).serialize(),
                        success: function (data) {
                            if (data.Code == 1000) {
                                $("#modalAdd").modal('hide');
                                ShowMessage(data.Message);
                                t.draw();
                            } else {
                                ShowMessageError(data.Message);
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            if (errorThrown === 'Unauthorized') {

                                ShowMessageError("Session 过期，请重新登录");

                                window.location.href = "/Account/Login";

                            } else {
                                ShowMessageError(XMLHttpRequest.responseText);

                            }

                        }
                    });

                }
            });

            //参数校验
            $('#task_form_edit').validate({
                focusInvalid: false,

                rules: {
                    JobName: {
                        required: true
                    },
                    JobGroup: {
                        required: true
                    },
                    BeginTime: {
                        required: true
                    }
                },
                messages: {

                },
                submitHandler: function (form) {
                    console.log($(form).serialize());
                    $.ajax({
                        type: "POST",
                        url: "/Task/ModifyTask",


                        data: $(form).serialize(),
                        success: function (data) {
                            if (data.Code == 1000) {
                                $("#modalEdit").modal('hide');
                                ShowMessage(data.Message);
                                t.draw();
                            } else {
                                ShowMessageError(data.Message);
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            if (errorThrown === 'Unauthorized') {

                                ShowMessageError("Session 过期，请重新登录");

                                window.location.href = "/Account/Login";

                            } else {
                                ShowMessageError(XMLHttpRequest.responseText);

                            }
                        }
                    });

                }
            });

            $("#triggerType").change(function () {
                if ($(this).val() == 1) {
                    $("#cron_div").show();
                    $("#cron").rules("add", {
                        required: true
                    });
                    $("#repeatTimes").val('');
                    $("#interval").val('');

                    $("#repeatTimes_div").hide();
                    $("#interval_div").hide();
                } else {
                    $("#cron_div").hide();
                    $("#repeatTimes_div").show();
                    $("#repeatTimes").rules("add", {
                        required: true
                    });
                    $("#cron").val('');
                    $("#interval_div").show();
                }
            });

            $("#triggerType_edit").change(function () {
                if ($(this).val() == 1) {
                    $("#cron_div_edit").show();
                    $("#cron_edit").rules("add", {
                        required: true
                    });
                    $("#repeatTimes_div_edit").hide();
                    $("#interval_div_edit").hide();

                    $("#repeatTimes_edit").val('');
                    $("#interval_edit").val('');

                } else {
                    $("#cron_div_edit").hide();
                    $("#repeatTimes_div_edit").show();
                    $("#repeatTimes_edit").rules("add", {
                        required: true
                    });
                    $("#interval_div_edit").show();
                    $("#cron_edit").val('');
                }
            });

            connection.on("ReceiveMessage", (user, message) => {
                console.log("---------------", message);
                if (message == "RefreshTask") {
                    t.draw();
                }

            });


        });

        //修改详细
        function editDetail(name, group) {
            $.ajax({
                url: "/Task/GetTaskDetail",
                data: { name: name, group: group },
                type: "post",
                success: function (data) {

                    var schedule = data.Data;
                    $("#jobName_edit").val(schedule.JobName);
                    $("#jobGroup_edit").val(schedule.JobGroup);
                    $("#triggerType_edit").val(schedule.TriggerType);
                    $("#jobDescription_edit").val(schedule.JobDescription);

                    if (schedule.TriggerType == 1) {
                        $("#cron_edit").val(schedule.Cron);
                        $("#cron_div_edit").show();
                        $("#cron_edit").rules("add", {
                            required: true
                        });
                        $("#repeatTimes_div_edit").hide();
                        $("#interval_div_edit").hide();
                    } else {
                        $("#repeatTimes_edit").val(schedule.RepeatTimes);
                        $("#repeatTimes_div_edit").show();
                        $("#repeatTimes_edit").rules("add", {
                            required: true
                        });
                        $("#interval_div_edit").show();
                        $("#interval_edit").val(schedule.Interval);
                    }
                    $("#beginTime_edit").val(dataTime(schedule.BeginTime));
                    $("#endTime_edit").val(schedule.EndTime == null ? null : dataTime(schedule.EndTime));
                    $("#assemblyName_edit").val(schedule.AssemblyName);
                    $("#insideClass_edit").val(schedule.ClassName);
                    $("#params_edit").val(schedule.Params);
                }
            });
        }

        //查看详细
        function viewDetail(name, group) {
            $.ajax({
                url: "/Task/GetTaskDetail",
                data: { name: name, group: group },
                type: "post",
                success: function (data) {

                    var schedule = data.Data;
                    $("#jobName_view").val(schedule.JobName);
                    $("#jobGroup_view").val(schedule.JobGroup);
                    $("#triggerType_view").val(schedule.TriggerType);
                    $("#jobDescription_view").val(schedule.JobDescription);

                    if (schedule.TriggerType == 1) {
                        $("#cron_view").val(schedule.Cron);
                        $("#cron_div_view").show();

                        $("#repeatTimes_div_view").hide();
                        $("#interval_div_view").hide();
                    } else {

                        $("#repeatTimes_div_view").show();
                        $("#interval_div_view").show();
                        $("#repeatTimes_view").val(schedule.RepeatTimes);


                        $("#interval_view").val(schedule.Interval);
                    }
                    $("#beginTime_view").val(dataTime(schedule.BeginTime));
                    $("#endTime_view").val(schedule.EndTime == null ? null : dataTime(schedule.EndTime));
                    $("#assemblyName_view").val(schedule.AssemblyName);
                    $("#insideClass_view").val(schedule.ClassName);
                    $("#params_view").val(schedule.Params);
                }
            });
        }

        //停止任务
        function stopTask(name, group) {
            $.ajax({
                url: "/Task/StopTask",
                data: { name: name, group: group },
                type: "post",
                success: function (data) {
                    if (data.Code == 1000) {
                        $("#js_table").DataTable().draw();

                        ShowMessage(data.Message);
                    } else {

                        ShowMessageError(data.Message);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    if (errorThrown === 'Unauthorized') {

                        ShowMessageError("Session 过期，请重新登录");

                        window.location.href = "/Account/Login";

                    } else {
                        ShowMessageError(XMLHttpRequest.responseText);

                    }
                }
            });
        }
        //恢复暂停任务
        function resumeTask(name, group) {
            $.ajax({
                url: "/Task/ResumeTask",
                data: { name: name, group: group },
                type: "post",
                success: function (data) {
                    if (data.Code == 1000) {
                        $("#js_table").DataTable().draw();
                        ShowMessage(data.Message);
                    } else {

                        ShowMessageError(data.Message);
                    }
                }
            });
        }
        //删除任务
        function deleteTask(name, group) {
            $("#deleteHaulBtn").click(function () {
                $.ajax({
                    url: "/Task/DeleteTask",
                    data: { name: name, group: group },
                    type: "post",
                    success: function (data) {
                        if (data.Code == 1000) {
                            $("#js_table").DataTable().draw(); $("#delcfmOverhaul").modal('hide');
                            ShowMessage(data.Message);
                        } else {

                            ShowMessageError(data.Message);
                        }
                    }
                });
            })
        }
        //时间戳转换
        function dataTime(time) {
            // 比如需要这样的格式 yyyy-MM-dd hh:mm:ss
            var _date = new Date(time),
                year = _date.getFullYear(), //年
                month = _date.getMonth() + 1, //月
                day = _date.getDate(), //日
                hour = _date.getHours(), //时间
                minute = _date.getMinutes(), //分钟
                second = _date.getSeconds();//秒
            month = month < 10 ? "0" + month : month;
            day = day < 10 ? "0" + day : day;
            minute = minute < 10 ? "0" + minute : minute;
            return year + "-" + month + "-" + day + " " + hour + ":" + minute;
        }
        //清空表单
        function resetForm() {
            $('#task_form_add')[0].reset();
            $("#repeatTimes").rules("add", {
                required: true
            });
            $("#interval").rules("add", {
                required: true
            });
            $("#triggerType").val("2");
            $("#repeatTimes_div").show();
            $("#interval_div").show();
            $("#cron_div").hide();
        }
    </script>
}
